using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

using DBA.DataBaseAccess;
using Common.ExpressionAnalysis;

namespace BL.ServerBL
{
    partial class ServerRemotingCommon : BL.BPClientRemotingInterface.IBudgetPay
    {

        public bool IsLegal(string pay_price, string budget_type_name)
        {
            if (!StringAnalysis.IsNumeric(pay_price) ||
                DBAccess.GetBudgetTypeIdByBudgetTypeName(budget_type_name) == null)
                return false;
            return true;
        }


        public bool IsPayOverdraft(string pay_price, string budget_range_id,
            string department_id, string budget_type_id)
        {
            string budget_balance_sum = DBAccess.GetBudgetBalanceSum(budget_range_id, department_id, budget_type_id);
            return Convert.ToInt32(pay_price) <= Convert.ToInt32(budget_balance_sum);
        }

        public bool RequestPayBorrowPayBack(string borrow_id, string pay_price, string user_id,
            string pay_remark, 
             string[,] pay_item_info)
        {
            if (DBAccess.InsertPayLog(borrow_id, pay_price, user_id, pay_remark, true, pay_item_info))
            {
                return true;
            }
            else
                return false;
        }
        
        public bool ExecutePayBorrowPayBack(string pay_log_id)
        {
            DataSet ds = DBAccess.GetPayLogById(pay_log_id);
            if (ds.Tables[0].Rows.Count == 0)
                return false;
            string pay_price = ds.Tables[0].Rows[0]["pay_price"].ToString().Trim();
            string borrow_id = ds.Tables[0].Rows[0]["borrow_id"].ToString().Trim();
            if (this.ExecutePayBorrowPayBack(borrow_id, pay_price))
                return DBAccess.ChangePayState(pay_log_id, "2");
            else
                return false;
        }

        public bool ExecutePayBorrowPayBack(string borrow_id, string pay_price)
        {
            DBAccess.ChangeBorrowPayPriceSub(borrow_id, pay_price);
            if (DBAccess.GetPriceNotPayBack(borrow_id) == "0")
                DBAccess.ChangeBorrowIsAllPayBack(borrow_id, true);
            return true;
        }

        public bool ExecuteBudgetPay(string pay_log_id)
        {
            DataSet ds = DBAccess.GetPayLogById(pay_log_id);
            if (ds.Tables[0].Rows.Count == 0)
                return false;
            string pay_price = ds.Tables[0].Rows[0]["pay_price"].ToString().Trim();
            string budget_id = ds.Tables[0].Rows[0]["budget_id"].ToString().Trim();
            if (this.ExecuteBudgetPay(budget_id, pay_price))
                return DBAccess.ChangePayState(pay_log_id, "2");
            else
                return false;
        }

        public bool ExecutCashBorrowPayBack(string borrow_id, string cash)
        {
            DBAccess.ChangeBorrowCashPriceSub(borrow_id, cash);
            if (DBAccess.GetPriceNotPayBack(borrow_id) == "0")
                DBAccess.ChangeBorrowIsAllPayBack(borrow_id, true);
            string budget_id = DBAccess.GetBudgetIdOfBorrowInfo(borrow_id);
            DBAccess.ChangeBudgetBalanceSub(budget_id, Convert.ToInt32(cash));
            return true;
        }
        public bool ExecuteBudgetPay(string budget_id, string pay_price)
        {
            return DBAccess.ChangeBudgetBalanceSub(budget_id, (-1) * Convert.ToInt32(pay_price));
        }


        public bool RequestBudgetPay(string user_id, string pay_price,
            string budget_id,string pay_remark, string[,] pay_item_info)
        {
            return DBAccess.InsertPayLog(budget_id, pay_price, user_id, pay_remark, false, pay_item_info);
        }

        public bool PayLogCheckAg(string pay_log_id)
        {
            string pay_state_id = "0";
            return DBAccess.ChangePayState(pay_log_id, pay_state_id);
        }

        public bool SetPayLogChecked(string pay_log_id)
        {
            string pay_state_id = "1";
            return DBAccess.ChangePayState(pay_log_id, pay_state_id);
        }

        public DataSet GetPayFullLogChecked()
        {
            string pay_state_id = "1";
            return DBAccess.GetPayFullLog(pay_state_id);
        }

        public DataSet GetBorrowPayFullLogChecked()
        {
            string pay_state_id = "1";
            return DBAccess.GetBorrowPayFullLog(pay_state_id);
        }
        public DataSet GetPayFullLogPayed()
        {
            string pay_state_id = "2";
            return DBAccess.GetPayFullLog(pay_state_id);
        }

        public DataSet GetBorrowPayFullLogPayed()
        {
            string pay_state_id = "2";
            return DBAccess.GetBorrowPayFullLog(pay_state_id);
        }

        public DataSet GetPayLog()
        {
            return null;
        }

        public DataSet GetPayPriceLog()
        {
            return DBAccess.GetPayPriceLog();
        }

        public DataSet GetBudgetItemFullInfo(string budget_type_id, string department_id, string budget_range_id )
        {
            return DBAccess.GetBudgetItemFullInfo(budget_type_id, department_id, budget_range_id, true);
        }

        public DataSet GetPayItemFullInfo(string pay_log_id)
        {
            return DBAccess.GetPayItemFullInfo(pay_log_id);
        }
        public DataSet GetPayLogFullInfo(string pay_log_id)
        {
            return DBAccess.GetPayLogFullInfo(pay_log_id);

        }

        

        
    }
}
